package com.wechat.service;

import com.github.binarywang.wxpay.bean.entpay.EntPayResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.wechat.dao.UserWithdrawcashRecordsDao;
import com.wechat.entity.User;
import com.wechat.entity.UserBalanceRecord;
import com.wechat.entity.UserWithdrawcashRecords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 *
 * @author cgc
 * @date 18/4/11
 */
@Service
public class UserWithdrawcashService {

    @Autowired
    private UserWithdrawcashRecordsDao userWithdrawcashRecordsDao;
    @Autowired
    private PayService payService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserBalanceService userBalanceService;

    /**
     *
     */
    public int deleteById(Long id) {
        return userWithdrawcashRecordsDao.deleteById(id);
    }

    /**
     *
     */
    public int save(UserWithdrawcashRecords record) {
        return userWithdrawcashRecordsDao.save(record);
    }

    /**
     *
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public UserWithdrawcashRecords findById(Long id) {
        return userWithdrawcashRecordsDao.findById(id);
    }

    /**
     *
     */
    public int update(UserWithdrawcashRecords record) {
        return userWithdrawcashRecordsDao.update(record);
    }

    /**
     *
     */
    @Transactional(readOnly = true,rollbackFor = Exception.class)
    public List<UserWithdrawcashRecords> list(Integer userId,Integer dataState,Date payTimeBegin,Date payTimeEnd,
                                              Date createTimeBegin, Date createTimeEnd) {
        return userWithdrawcashRecordsDao.list(userId,dataState,payTimeBegin,payTimeEnd,createTimeBegin,createTimeEnd);
    }


    /**
     *
     * @param user
     * @param amount 提现金额(正数)
     * @return
     * @throws WxPayException
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean withdrawCash(User user,BigDecimal amount) throws WxPayException{

        UserWithdrawcashRecords withdrawcashRecord = new UserWithdrawcashRecords();
        withdrawcashRecord.setBalance(user.getBalance());
        withdrawcashRecord.setUserId(user.getId());
        withdrawcashRecord.setRealFee(amount.negate());
        withdrawcashRecord.setTotalFee(amount.negate());

        this.save(withdrawcashRecord);

        //增加账户财务流水并扣除余额
        UserBalanceRecord balanceRecord = new UserBalanceRecord();
        balanceRecord.setBalance(user.getBalance());
        balanceRecord.setUserId(user.getId());
        balanceRecord.setAmount(amount.negate());
        balanceRecord.setTitle("提现-用户余额提现");
        balanceRecord.setRecordType(UserBalanceRecord.RECORD_TYPE_WITHDRAWCASH);
        userBalanceService.save(balanceRecord);
        userService.updateBalance(user.getId(),amount.negate());

        return true;
    }

}
